package com.springbootbase.springbootproject.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.springbootbase.springbootproject.mapper.SupervisorMapper;
import com.springbootbase.springbootproject.pojo.Evaluation;
import com.springbootbase.springbootproject.pojo.MentorData;
import com.springbootbase.springbootproject.service.SupervisorService;
import com.springbootbase.springbootproject.utils.HttpClientUtils;
import com.springbootbase.springbootproject.utils.JsonUtil;
import com.springbootbase.springbootproject.utils.apiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SupervisorServiceImpl implements SupervisorService {

    @Autowired
    private SupervisorMapper supervisorMapper ;

    @Override
    public List getUniversityByLevel(String level) {
        Map jsonObject = null;
        try {
            jsonObject = JsonUtil.nioMethod("src/main/resources/static/level.json");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return (List) jsonObject.get(level);
    }

    @Override
    public List getDepartmentByUniversity(String university) {
        Map jsonObject = null;
        try {
            jsonObject = JsonUtil.nioMethod("src/main/resources/static/university.json");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return (List) jsonObject.get(university);
    }

    @Override
    public List<String> getSupervisorByDepartment(String university, String department) {
//        Map map = new HashMap<>();
//        map.put("university",university);
//        map.put("department",department);
//        String url = "https://www.daoshipingjia.org/ds/conn.php";
//        String string = HttpClientUtils.doGet(url,map);
//        Map parseObject = JSONObject.parseObject(string);
        List<String> name = supervisorMapper.getSupervisor(university,department);
        return  name;
    }

    @Override
    public ArrayList<Evaluation> getReview(String university, String department, String supervisor) {
      ArrayList<Evaluation> evaluationArrayList =  supervisorMapper.getComment(university,department,supervisor);
        return  evaluationArrayList;
    }
    @Override
    public Map getReview2(String university, String department, String supervisor) {
        Map map = new HashMap<>();
        map.put("university",university);
        map.put("department",department);
        map.put("supervisor",supervisor);
        map.put("code","券码OR密码");
        String url = "https://www.daoshipingjia.org/ds/conn.php";
        String string = HttpClientUtils.doGet(url,map);
        Map parseObject = JSONObject.parseObject(string);
        return parseObject;
    }

    @Override
    public String getHindex(String id) {
        apiUtil apiUtils = new apiUtil();
        try {
            return apiUtils.getScholarInfo(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getId(String name) {
        apiUtil apiUtils = new apiUtil();
        try {
            return apiUtils.getScholarId(name);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public MentorData getReview3(String university, String department, String supervisor) {
        MentorData mentorData = supervisorMapper.getInfo(university,department,supervisor);
        return mentorData;
    }


}